//
//  ViewController.swift
//  持久化
//
//  Created by 李煜 on 2024/4/9.
//

import UIKit

class ViewController: UIViewController {
    @IBOutlet weak var nameTf: UITextField!
    
    @IBOutlet weak var ageTf: UITextField!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //        homeDirectoryPath()
        preferencePath()
    }
    
    ///沙盒home路径
    func homeDirectoryPath() {
        guard let dirpath = FileManager.default.urls(for: .applicationDirectory, in: .userDomainMask).first else{
            return
        }
        //打印目录
        print(dirpath)
        //  file:///Users/liyu/Library/Developer/CoreSimulator/Devices/0265A0FF-856D-4336-A06A-A59912B5ADF3/data/Containers/Data/Application/5F3AA094-A8A0-4F68-A9C9-2875902C8A55/Documents/
        
    }
    ///preference
    func preferencePath() {
        guard let path = FileManager.default.urls(for: .preferencePanesDirectory, in: .userDomainMask).first else { return  }
        print(path)
    }
    
    @IBAction func write(_ sender: UIButton) {
        let name = nameTf.text!
        let age = ageTf.text!
        
        UserDefaults.standard.set(name, forKey: "name")
        UserDefaults.standard.set(age, forKey: "age")
        
//        Swift中一般不需要使用synchronize
//        UserDefaults.standard.synchronize()
        
    }
    @IBAction func read(_ sender: Any) {
        guard let name = UserDefaults.standard.value(forKey: "name"),
              let age = UserDefaults.standard.value(forKey: "age") else{return}
        print("\(name) is \(age) years old.")
    }
}

